import java.util.TreeMap;

public class MyBinarySearchTree {
    public class Node{
        public int val;
        public Node left;
        public  Node right;
        public Node(int val){
            this.val = val;
        }
    }
    public Node root;
    //数据插入
    public Node insert(int val){
        if (root == null){
            root = new Node(val);
            return root;
        }
        Node parent = null;
        Node cur = root;
        Node newTree = new Node(val);
        while (cur != null){
            if (cur.val < val){
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val){
                parent = cur;
               cur =  cur.left;
            }else {
                return root;
            }
        }
        if (parent.val > val){
            parent.left = newTree;
        }else{
            parent.right = newTree;
        }
        return root;
    }
    public boolean find(int val){
        Node cur = root;
        while (cur != null){
            if (cur.val == val){
                return true;
            }else if(cur.val > val){
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        return false;
    }
    //中序遍历
    public void ergodic(Node cur){
        if (cur == null){
            return;
        }
        ergodic(cur.left);
        System.out.print(cur.val + " ");
        ergodic(cur.right);
    }
    //删除
    //删除函数
    public void delete(Node curParent,Node cur){
        if (cur.left == null){
            if (cur == root){
                root = root.right;
            }else{
                if (cur == curParent.left){
                    curParent.left = cur.right;
                }else {
                    curParent.right = cur.right;
                }
            }
        }else if(cur.right == null){
            if (cur == root){
                root = root.left;
            }else {
                if (cur == curParent.left){
                    curParent.left = cur.left;
                }else {
                    curParent.right = cur.left;
                }
            }
        }else {
            curParent = cur;
            Node tmp = cur.right;
            while (tmp.left != null){
                curParent = tmp;
                tmp = tmp.left;
            }
            cur.val = tmp.val;
            if (tmp == curParent.right){
                curParent.right = tmp.right;
            }else {
                curParent.left = tmp.right;
            }
        }
    }
    public boolean remove(int val){
        Node cur = root;
        Node curParent = null;
        while (cur != null){
            if (cur.val > val){
                curParent = cur;
                cur = cur.left;
            }else if(cur.val < val){
                curParent = cur;
                cur = cur.right;
            }else {
                delete(curParent,cur);
                return true;
            }
        }
        return false;
    }
}
